
var async = require('async');
var mysql = require('mysql');
var $conf = require('./db');
var $sql = require('./userSqlMapping');
/*function c(arg1,arg2){
	console.log(arg1+arg2);
}*/

// 使用连接池，提升性能
var pool  = mysql.createPool($conf.mysql);

pool.getConnection(function(err, connection) {
  // connected! (unless `err` is set) 
  if(err){
  	throw err
  }else{
  	console.log("connect success");
  }
});

// 向前台返回JSON方法的简单封装
var jsonWrite = function (res, ret) {
	if(typeof ret === 'undefined') {
		res.json({
			succ: false,
			code:'1',
			msg: '操作失败'
		});
	} else {
		res.json(ret);
	}
};
Date.prototype.format = function(date){
	return this.getFullYear()+'-'+(this.getMonth()+1)+'-'+this.getDate()+' '+this.getHours()+':'+this.getMinutes()+':'+this.getSeconds();
}


var delTask = [
	function(callback){
		pool.getConnection(function(err, connection) {
			var id = 5;
			connection.query($sql.queryById, id, function(err, result) {			
				if(result && result.length>0){
					console.log("getById:");
					console.log(result[0]);
					callback(err,result[0].id);
				}else{
					callback(err,null);
									
				}
				connection.release();
			});
		});		
	},
	function(getId,callback){
		console.log("getId: "+getId);
		pool.getConnection(function(err, connection) {
			if(getId==null){
				//jsonWrite(res,'不存在该ID');
				callback(null,'不存在该ID');
				connection.release();

			}else{
				var id = getId;
				console.log("delte id: "+id);
				connection.query($sql.delete, id, function(err, result) {
					if(result.affectedRows > 0){
						console.log("delete success");
						//jsonWrite(res,result);
						callback(err,result);
					}else{
						callback(err,null);
						connection.release();				
					}
				});
			}
		});
	}
];
async.waterfall(delTask,function(err,results){
	
	if(err){
		throw err;
	}else{
		console.log(results);
	}
});

